const express = require('express')
const querystring = require('querystring')
const request = require('request')

const app = express()

app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

app.get('/', (req,res) => { 
    res.send('hello World')
})
app.get('/all', (req, res) => { 
    res.send('get')
})

app.post('/postCity', (req, res) => {
    var city = ''
    for (var obj in req.body) {
        city = obj
    }  
    console.log(city)
    var queryData = querystring.stringify({
        "city": city,  // 接受短信的用户手机号码
        "key": '9d83e432ce432cb1311e2e8e303c3c5d',  // 应用APPKEY(应用详细页查询)
    })
    console.log('6')
    var queryUrl = 'http://apis.juhe.cn/simpleWeather/query?'+queryData;
    
    request(queryUrl, function (error, response, body) {
        // console.log(error)
        // console.log(body)
        if (error===null) {
            // console.log(body) // 打印接口返回内容
            
            var jsonObj = JSON.parse(body); // 解析接口返回的JSON内容
            console.log(jsonObj)
            res.end(body)
            // res.end(jsonObj)
        } else {
            console.log('请求异常');
        }
    }) 
})
app.listen(666, () => { 
    console.log('Server is running on port 666')
})